const key = 'NI4BZ-EO53C-FB22U-ARZ6F-GTJVF-P2FSH';

const tx_map = (latitude, longitude) => {
  return new Promise((reslove, reject) => {
    wx.request({
      url: 'https://apis.map.qq.com/ws/geocoder/v1/',
      data: {
        key,
        location: `${latitude},${longitude}`,
        output: 'json',
        coord_type: 5,
        get_poi: 0
      },
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=utf-8"
      },
      success: (res) => {
        wx.hideLoading()
        if (res.data.status == "0") {
          reslove({
            ...res.data.result,
            addressComponent: {
              ...res.data.result.address_component,
              adcode: res.data.result.ad_info.adcode,
              town: res.data.result.address_component.street
            },
            formatted_address: res.data.result.formatted_addresses.standard_address
          })
        }
      }
    })
  })
}

const baidu_map = (latitude, longitude) => {
  return new Promise((reslove, reject) => {
    wx.request({
      url: 'https://api.map.baidu.com/reverse_geocoding/v3',
      data: {
        ak: getApp().globalData.ak,
        location: `${latitude},${longitude}`,
        output: 'json',
      },
      method: "POST",
      header: {
        "content-type": "application/x-www-form-urlencoded;charset=utf-8"
      },
      success: (res) => {
        wx.hideLoading()
        if (res.data.status == 0) {
          reslove(res.data.result)
        } else {
          tx_map(latitude, longitude).then(result => {
            reslove(result)
          })
        }
      }
    })
  })
}



module.exports = {
  tx_map,
  baidu_map
}